package com.vecspace.data.numerical;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonSerialize(using = HNumericalJsonSerializer.class)  
@JsonDeserialize(using = HMoneyWanYuanJsonDeserializer.class) 
public class HMoneyWanYuan extends HMoney{
	private static final long serialVersionUID = 4021619080698151247L;
		
	public HMoneyWanYuan() {
		super();
		formatDigits = 2;
	}
	/**
	 * 
	 * @param value 金额（元）
	 */
	public HMoneyWanYuan(BigDecimal value) {
		this.setValue(value);
	}

	@Override
	public String getText() {
		String text = HMoney.formatWanYuan(this.getValue(), this.getSeparatorDigit(), formatDigits);
		return text;
	}
	
	@Override
	public void setText(String text) {
		BigDecimal num = this.parseDouble(text);
		num = num.multiply(BigDecimal.valueOf(10000));
		this.setValue(num);
	}
	
	@Override
	public Class<HMoneyWanYuan> returnedClass() {
		return HMoneyWanYuan.class;
	}

	@Override
	public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
			throws HibernateException, SQLException {
		BigDecimal value = rs.getBigDecimal(names[0]);
		HMoneyWanYuan obj = new HMoneyWanYuan(value);
		return obj;
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		if(value == null)return null;
		HMoneyWanYuan obj1 = (HMoneyWanYuan)value;
		HMoneyWanYuan obj2 = new HMoneyWanYuan(obj1.getValue());
		obj2.setSeparatorDigit(obj1.getSeparatorDigit());
		return obj2;
	}
	
	/**两个数相加，null相当于0
	 * 
	 * @param num1
	 * @param num2
	 * @return 
	 * @author: wanghua
	 */
	public static HMoneyWanYuan add(HMoneyWanYuan num1, HMoneyWanYuan num2) {
		BigDecimal decimal = HNumerical.add((HNumerical)num1, (HNumerical)num2);
		return new HMoneyWanYuan(decimal);
	}
	
	/**两个数相减，null相当于0
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyWanYuan subtract(HMoneyWanYuan num1, HMoneyWanYuan num2) {
		BigDecimal decimal = HNumerical.subtract((HNumerical)num1, (HNumerical)num2);
		return new HMoneyWanYuan(decimal);
	}
	
	/**两个数相乘，null相当于0
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyWanYuan multiply(HMoneyWanYuan num1, HNumber num2) {
		BigDecimal decimal = HNumerical.multiply((HNumerical)num1, (HNumerical)num2);
		if(decimal != null && num1 != null && num1.getValue() != null) {
			decimal = decimal.setScale(num1.getValue().scale(), RoundingMode.HALF_UP);
		}
		return new HMoneyWanYuan(decimal);
	}
	
	/**两个数相除，null相当于0
	 * 
	 * @param num1
	 * @param num2 
	 * @author: wanghua
	 */
	public static HMoneyWanYuan divide(HMoneyWanYuan num1, HNumber num2) {
		BigDecimal decimal = HNumerical.divide((HNumerical)num1, (HNumerical)num2);
		if(decimal != null && num1.getValue() != null) {
			decimal = decimal.setScale(num1.getValue().scale(), RoundingMode.HALF_UP);
		}
		return new HMoneyWanYuan(decimal);
	}
}
